package com.cimpm.model.service.impl;

import com.cimpm.model.entity.Node;
import com.cimpm.project.service.impl.ProjectnodeMapperServiceImpl;
import com.hby.common.callback.AutoProcessControllerPart;
import com.hby.common.entities.aspect.parameter.MapperParam;
import com.hby.common.entities.aspect.parameter.ServiceInfo;
import com.hby.common.exceptions.AutoException;
import com.hby.common.exceptions.ExceptionEnum;
import com.hby.common.services.Impl.mian.AutoEntityServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Service
public class NodeServiceImpl extends AutoEntityServiceImpl<Node> {

    @Autowired
    private ProjectnodeMapperServiceImpl projectnodeMapperService;

    @Override
    public void customService(AutoProcessControllerPart autoProcessControllerPart, ServiceInfo serviceInfo) throws Throwable {
        //获取
        autoProcessControllerPart.setBeforeGetPartCallback(() -> {
            if (!serviceInfo.getParamConverter().containsKey("projectId"))
                throw new AutoException(ExceptionEnum.PARAM_FIELD, "需包含projectId参数");
            Set<Integer> nodeIds = projectnodeMapperService.getSecondaryIdsFromPrimaryId(serviceInfo.getParamConverter().getInt("projectId"));
            //包装filterMap
            Map<String, Object> filterMap = new HashMap<>();
            filterMap.put("id", nodeIds);  //Collection格式，将筛选包含在该Collection中的实体
            //将filterMap添加至查询条件
            serviceInfo.getParamConverter().getParamMap().put("filterMap", filterMap);
            return null;
        });
        //添加
        autoProcessControllerPart.setBeforeAddPartCallback(() -> {
            if (!serviceInfo.getParamConverter().containsKey("projectId"))
                throw new AutoException(ExceptionEnum.PARAM_FIELD, "需包含projectId参数");
            return null;
        });
        autoProcessControllerPart.setAfterAddPartCallback(() -> {
            Integer newId = serviceInfo.getNewId();
            Integer projectId = serviceInfo.getParamConverter().getInt("projectId");
            projectnodeMapperService.addMapper(new MapperParam(projectId, newId));
            return null;
        });
    }
}
